package project.servlets;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import schronisko.Queries;

/**
 * Servlet implementation class Book
 */
@WebServlet("/Book")
public class Book extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	Queries q = new Queries();
	HttpSession s;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Book() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		s = request.getSession();
		String adding = request.getParameter("adding");
		String ID_client = request.getParameter("ID");
		if(adding != null) {
			int ID = Integer.parseInt(adding);
			int rows = q.Book(ID);
			if(rows > 0) {
				s.setAttribute("adding", 1);
			}
			else s.setAttribute("adding", 2);
			response.sendRedirect("user/add.jsp");
		}
		else if(ID_client != null){
			q.deleteBook(Integer.parseInt(ID_client));
			s.setAttribute("deleted", 1);
			response.sendRedirect("worker/added.jsp");
		}
	}

}
